package com.example.stuasist;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.stuasist.db.stuasistDbOpenHelper;
import com.example.stuasist.db.dao.CourseDao;
import com.example.stuasist.domain.Course;

public class MainActivity extends Activity {
	private GridView MainActivity_mygrid = null;
	private Button MainActivity_bt_takein = null;
	private LayoutInflater inflater;
	private List<Course> courses;
	private Map<Integer, Course> mis;
	ItemAdpter adapter;

	private int getDay(String day) {
		String[] name = new String[] { "", "星期一", "星期二", "", "星期三", "星期四",
				"星期五", "星期六", "星期日" };
		for (int i = 0; i < name.length; i++)
			if (name[i].equals(day))
				return i;
		return 0;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题栏
		super.setContentView(R.layout.activity_main);
		this.MainActivity_mygrid = (GridView) super
				.findViewById(R.id.MainActivity_mygrid);
		this.MainActivity_mygrid
				.setOnItemLongClickListener(new MyLongClickListener()); // 设置长按监听
		this.MainActivity_bt_takein = (Button) findViewById(R.id.MainActivity_bt_takein);
		this.MainActivity_bt_takein
				.setOnClickListener(new OnClickListenerimp1()); // 设置单机事件监听

		// 得到inflater的服务
		inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		

		adapter = new ItemAdpter(this);

		MainActivity_mygrid.setAdapter(adapter); // 调用ItemAdpter
		MainActivity_mygrid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {
				Course course = mis.get(position);
				if (course == null || "".equals(course.getCourse_name())) {
					return;
				}
				Intent startIntent = new Intent(MainActivity.this,
						information.class);
				String week_day = course.getWeek_day();
				int day_no = course.getDay_no();

				startIntent.putExtra("T_week_day", week_day);
				startIntent.putExtra("T_day_no", day_no);

				System.out.println("main T_week_day = " + week_day);
				System.out.println("main T_day_no = " + day_no);

				startActivity(startIntent);
			}
		});
		
		
		stuasistDbOpenHelper helper = new stuasistDbOpenHelper(this);

		helper.getReadableDatabase();

		CourseDao dao = new CourseDao(this);

		courses = dao.getAllCourses();
		
		mis = new HashMap<Integer, Course>();

		int len = courses.size();
		for (int i = 0; i < len; i++) {
			Course cource = courses.get(i);
			int row = cource.getDay_no();
			int col = getDay(cource.getWeek_day());
			mis.put((row - 1) * 7 + col - 1, cource);
		}

	}

	

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//adapter.notify();
		// 得到inflater的服务
		System.out.println("onResume");
		
		
		stuasistDbOpenHelper helper = new stuasistDbOpenHelper(this);

		helper.getReadableDatabase();
		
		CourseDao dao = new CourseDao(MainActivity.this);

		courses = dao.getAllCourses();
		
		mis = new HashMap<Integer, Course>();

		int len = courses.size();
		for (int i = 0; i < len; i++) {
			Course cource = courses.get(i);
			int row = cource.getDay_no();
			int col = getDay(cource.getWeek_day());
			mis.put((row - 1) * 7 + col - 1, cource);
		}
		
		adapter.notifyDataSetChanged();
		
	}



	public boolean onKeyDown(int keyCode, KeyEvent event) { // 设置监听返回键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			MainActivity.this.finish(); // 退出程序
		}
		return false;
	}

	private class OnClickListenerimp1 implements OnClickListener { // 单击事件
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(MainActivity.this, take_in.class); // 实现页面跳转
			startActivity(intent);
			
			
		}
	}

	private class MyLongClickListener implements OnItemLongClickListener {
		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			Dialog dialog = new AlertDialog.Builder(MainActivity.this) // 设置弹出对话框
					.setTitle("Message") // 设置弹出对话框标题
					.setMessage("是否清空课表内容?") // 设置弹出对话框内容
					.setIcon(R.drawable.pic4) // 设施弹出对话框标题图片
					.setPositiveButton("确定", // 设置确定按钮
							new DialogInterface.OnClickListener() { // 单击确定按钮的监听
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									System.out.println("清空表");
									CourseDao dao = new CourseDao(MainActivity.this);
									dao.deleteAll();	
									stuasistDbOpenHelper helper = new stuasistDbOpenHelper(MainActivity.this);

									helper.getReadableDatabase();
									

									courses = dao.getAllCourses();
									
									mis = new HashMap<Integer, Course>();

									int len = courses.size();
									for (int i = 0; i < len; i++) {
										Course cource = courses.get(i);
										int row = cource.getDay_no();
										int col = getDay(cource.getWeek_day());
										mis.put((row - 1) * 7 + col - 1, cource);
									}
									
									adapter.notifyDataSetChanged();
									
									
									
								}
							}).setNeutralButton("取消", // 设置取消按钮
							new DialogInterface.OnClickListener() { // 单击取消按钮的监听
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									System.out.println("取消清除表");

								}
							}).create();
			dialog.show(); // 显示对话框
			return true;
		}
	}

	public class ItemAdpter extends BaseAdapter {

		private String[] texts = { "   ", "Mon", "Tue", "Wed", "Thu", "Fri",
				"Sat", "Sun" };

		private Context context; // 定义Context

		public ItemAdpter(Context context) {
			this.context = context;
		}

		@Override
		public int getCount() { // 设置GridView的行列数
			return 8 * 13;
		}

		public Object getItem(int position) { // 获取位置
			return position;
		}

		@Override
		public long getItemId(int id) {
			return id;
		}

		@SuppressLint("NewApi")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView != null) {
				// System.out.println(convertView.getClass());
			}
			View view = inflater.inflate(R.layout.item, null);
			TextView tv = (TextView) view.findViewById(R.id.tv_item);
			// TextView tv = new TextView(MainActivity.this);
			String course_name = null;
			Course course = mis.get(position);
			// if(course.getCourse_name() == null ){
			// course_name = null;
			// }else{
			if (course != null) {
				course_name = course.getCourse_name();
			}
			//System.out.println("position = " + position);
			//System.out.println("name = " + course_name);
			tv.setText(course_name);
			/*
			 * String [] name = new
			 * String[]{"","星期一","星期二","","星期三","星期四","星期五","星期六","星期日"};
			 * 
			 * for (int i = 1; i <=name.length ; i++){
			 * if(week_day.equals(name[i])){ int row = course_day; int col = i;
			 * int pos = ((row - 1) * 8) + col;
			 * 
			 * 
			 * } }
			 */

			// int course_nameInt = Integer.parseInt(course_name);
			// System.out.println("course_name--"
			// +course_name+"-course_day-"+course_day);

			/* tv.setText(position + ""); */
			tv.setTextSize(15);
			tv.setTextColor(Color.BLACK); // 设置显示内容字体为黑色
			// tv.setGravity(Gravity.CENTER); // 设置显示位置为居中
			tv.setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);
			tv.setBackgroundColor(Color.WHITE); // 设置背景颜色为白色
			tv.setLayoutParams(new RelativeLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

			return view;
		}
	}
}
